package com.kushal.published.runtime;
/**
 * @Author Kushal Paudyal
 * www.sanjaal.com/java
 * 
 * This class invokes some external commands.
 * For demo, we are invoking the window's ping command.
 */
import java.io.*;

public class RunExternalCommands {

	public static void main(String argv[]) {
		try {
			String line;
			/**
			 * getRunTime() Returns the runtime object associated with the current 
			 * Java application. Most of the methods of class Runtime are instance 
			 * methods and must be invoked with respect to the current runtime object. 
			 * 
			 * exec() method Executes the specified string command in a separate 
			 * process. The command argument is parsed into tokens and then 
			 * executed as a command in a separate process. The token 
			 * parsing is done by a java.util.StringTokenizer created by the call.
			 * 
			 */
			Process p = Runtime.getRuntime().exec(
					"C:/windows/system32/ping.exe sanjaal.com");
			
			/**
			 * Create a buffered reader from the Process' input stream.
			 */
			BufferedReader input = new BufferedReader(new InputStreamReader(p
					.getInputStream()));
			
			/**
			 * Loop through the input stream to print the program output into console.
			 */
			while ((line = input.readLine()) != null) {
				System.out.println(line);
			}
			/**
			 * Finally close the reader
			 */
			input.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/*
	 * SANJAAL CORPS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF 
	 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
	 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
	 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SANJAAL CORPS SHALL NOT BE LIABLE FOR 
	 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR 
	 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. 
	 * 
	 * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE 
	 * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE 
	 * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT 
	 * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE 
	 * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE 
	 * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE 
	 * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). SANJAAL CORPS 
	 * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR 
	 * HIGH RISK ACTIVITIES. 
	 */
}
